package com.ymatou.iapi.sellercs.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.sellercs.parameter.GetHistoryMessageListBean;
import com.ymatou.iapi.sellercs.parameter.SendMessageBean;
import com.ymatou.iapi.sellercs.parameter.SessionResp;
import com.ymatou.iapi.sellercs.service.GetHistoryMessageListCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.model.YmtCssession;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*获取用户历史会话列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetHistoryMessageList{
	private static GetHistoryMessageListBean gethistorymessagelistBean;
	private static GetHistoryMessageListCall gethistorymessagelistCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	private static YmtSellerCSIMWapper YmtSellerCSIMWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户历史会话列表");
	}

	@Before
	public void caseUp() throws ClientProtocolException, IOException {
		gethistorymessagelistBean=new GetHistoryMessageListBean();
		gethistorymessagelistCall=new GetHistoryMessageListCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
		SellerCSIMCallService.OffLine(sellerId, null);
		SellerCSIMCallService.OffLine(zsellerId, null);
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gethistorymessagelistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetHistoryMessageList_001() {
		Logger.start(true,"获取用户历史会话列表,成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			// APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			SellerCSIMCallService.Assign();
			Thread.sleep(2000);
			SellerCSIMCallService.OffLine(sellerId, null);
			Thread.sleep(2000);
			List<YmtCssession> cinfos = YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			SellerCSIMCallService.MoveClosesessionToHistorySessionTable();
			Thread.sleep(2000);
			gethistorymessagelistBean.setCSId(sellerId);
			gethistorymessagelistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy/MM/dd HH:mm:ss"));
			gethistorymessagelistBean.setPageSize(20);
			gethistorymessagelistCall.setData(gethistorymessagelistBean);
			gethistorymessagelistCall.callService();			
			Logger.verifyEquals("200", gethistorymessagelistCall.getString("Status"), "验证Status");
			Logger.debug(FormatUtil.beanToGSONString(gethistorymessagelistCall.getResult()));
			ArrayList<SessionResp> result = gethistorymessagelistCall.getResult();
			Logger.debug(FormatUtil.beanToGSONString(gethistorymessagelistCall.getResult()));
			Logger.verifyEquals(gethistorymessagelistBean.getPageSize(), result.size(), "验result的size");
			Logger.verifyNotNull(result.get(0).getUserSessionId(), "验证getUserSessionId");
			Logger.verifyEquals(1,result.get(0).getUnReadMessageCount(), "验证getUnReadMessageCount");
			//?这里应该是全部的message数量？
//			Logger.verifyEquals(1,result.get(0).getAllMessageCount(), "验证getAllMessageCount");
			Logger.verifyEquals(false,result.get(0).getIsImportant(), "验证getIsImportant");
			Logger.verifyEquals(sendmessageBean.getMessage(),result.get(0).getLatestMessage().getMessage(), "验证getMessage");
			Logger.verifyEquals(userId,result.get(0).getToUserId(), "验证getToUserId");
			Logger.verifyEquals(JUserCallService.GetUserInfo(userId).getLoginId(),result.get(0).getToUserName(), "验证getToUserName");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetHistoryMessageList_002() {
		Logger.start(true,"UserNameKeyWord过滤成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			// APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			SellerCSIMCallService.SellerSendMessage(sellerId, userId, "lastmsg");
			SellerCSIMCallService.Assign();
			Thread.sleep(2000);
			SellerCSIMCallService.SetMessageSessionImportant(userId, sellerId);
			SellerCSIMCallService.OffLine(sellerId, null);
			Thread.sleep(2000);
			List<YmtCssession> cinfos = YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			SellerCSIMCallService.MoveClosesessionToHistorySessionTable();
			Thread.sleep(2000);
			gethistorymessagelistBean.setUserNameKeyWord(regbean.getLoginId());
			gethistorymessagelistBean.setCSId(sellerId);
			gethistorymessagelistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy/MM/dd HH:mm:ss"));
			gethistorymessagelistBean.setPageSize(20);
			gethistorymessagelistCall.setData(gethistorymessagelistBean);
			gethistorymessagelistCall.callService();			
			Logger.verifyEquals("200", gethistorymessagelistCall.getString("Status"), "验证Status");
			ArrayList<SessionResp> result = gethistorymessagelistCall.getResult();
			Logger.debug(FormatUtil.beanToGSONString(gethistorymessagelistCall.getResult()));
			Logger.verifyEquals(1, result.size(), "验result的size");
			Logger.verifyNotNull(result.get(0).getUserSessionId(), "验证getUserSessionId");
			Logger.verifyEquals(1,result.get(0).getUnReadMessageCount(), "验证getUnReadMessageCount");
			Logger.verifyEquals(2,result.get(0).getAllMessageCount(), "验证getAllMessageCount");
			Logger.verifyEquals(true,result.get(0).getIsImportant(), "验证getIsImportant");
			Logger.verifyEquals("lastmsg",result.get(0).getLatestMessage().getMessage(), "验证getMessage");
			Logger.verifyEquals(userId,result.get(0).getToUserId(), "验证getToUserId");
			Logger.verifyEquals(JUserCallService.GetUserInfo(userId).getLoginId(),result.get(0).getToUserName(), "验证getToUserName");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}